package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.system.model.dto.DictionaryDTO;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.system.convert.DictionaryConvert;
import com.xuecheng.system.entity.Dictionary;
import com.xuecheng.system.mapper.DictionaryMapper;
import com.xuecheng.system.service.DictionaryService;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {


    /*
    * 根据code查询数据字典
    * */
    @Override
    public DictionaryDTO getDictionary(String code) {

        //判断数据是否合法
        if(StringUtil.isBlank(code)){
            return null;
        }

        //构造查询条件
        LambdaQueryWrapper<Dictionary> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Dictionary::getCode,code);

        //查询一个
        Dictionary dictionary = this.getOne(queryWrapper);

        //判断查询到的数据是否为空
        DictionaryDTO dictionaryDTO = null;
        if(!(null == dictionary)){
            //Po转为Dto
            dictionaryDTO = DictionaryConvert.INSTANCE.entity2Dto(dictionary);
        }else {
            //new一个空的对象
            dictionaryDTO = new DictionaryDTO();
        }

        //返回数据
        return dictionaryDTO;
    }


    /*
    * 查询全部
    * */
    @Override
    public List<DictionaryDTO> queryAll() {

        //查询全部
        List<Dictionary> list = list();

        //Po转换DTO
        List<DictionaryDTO> dictionaryDTOList = DictionaryConvert.INSTANCE.entitys2Dtos(list);

        //返回数据
        return dictionaryDTOList;
    }
}
